﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace XUtility
{
    public static class ParamClass
    {
        #region 变量
        private const String AjaxFun = "fun";
        #endregion

        #region 获取请求数据
        #region 获取 String 类型参数
        /// <summary>
        /// 获取 String 类型参数
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static String RequestString(String sParam, String defaultValue = "")
        {
            return (HttpContext.Current.Request[sParam] == null ? defaultValue : HttpContext.Current.Request[sParam].ToString().Trim());
        }
        #endregion

        #region Int32 类型参数

        /// <summary>
        /// 获取 Int32 类型参数
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static Int32 RequestInt(String sParam, Int32 defaultValue = 0)
        {
            Int32 iValue;
            String sValue = RequestString(sParam);

            if (!Int32.TryParse(sValue, out iValue))
                return defaultValue;
            return iValue;
        }
        /// <summary>
        /// 获取 可空Int32 类型参数
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static Int32? RequestInt(String sParam, Int32? defaultValue)
        {
            Int32 iValue;
            String sValue = RequestString(sParam);

            if (String.IsNullOrWhiteSpace(sValue))
                return defaultValue;
            else
            {
                if (!Int32.TryParse(sValue, out iValue))
                    return defaultValue;
            }
            return iValue;
        }
        #endregion

        #region DateTime 类型参数
        
        /// <summary>
        /// 获取 DateTime 类型参数
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns></returns>
        public static DateTime RequestDateTime(String sParam)
        {
            return RequestDateTime(sParam, DateTime.Now).Value;
        }
        /// <summary>
        /// 获取 可空DateTime 类型参数
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static DateTime? RequestDateTime(String sParam, DateTime? defaultValue)
        {
            DateTime dValue;
            String sValue = RequestString(sParam);

            if (String.IsNullOrWhiteSpace(sValue) || !DateTime.TryParse(sValue, out dValue) || (dValue.Year == 0001))
            {
                if (defaultValue.HasValue)
                {
                    if (sParam.StartsWith("date"))
                    {
                        defaultValue = defaultValue.Value.Date;
                    }
                    else if (sParam.StartsWith("datt"))
                    {
                        defaultValue = defaultValue.Value;
                    }
                }
                return defaultValue;
            }
            return dValue;
        }
        #endregion

        #region Boolean 类型参数


        /// <summary>
        /// 把请求参数转换为Boolean类型
        /// <para>注释：</para>
        /// <para>  这个方法主要用于获取checkbox控件的值</para>
        /// <para>   true:[非空]  false:[null | "" | "off" | "0"]</para>
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns>参数值</returns>
        public static Boolean RequestBoolean(String sParam)
        {
            Boolean bValue = true;
            String sValue = RequestString(sParam);

            if (String.IsNullOrWhiteSpace(sValue) || ("false".Equals(sValue, StringComparison.OrdinalIgnoreCase)) || ("off".Equals(sValue, StringComparison.OrdinalIgnoreCase)) || ("0".Equals(sValue)))
                bValue = false;
            return bValue;
        }
        /// <summary>
        /// 把请求参数转换为Boolean类型
        /// <para>注释：</para>
        /// <para>  这个方法主要用于获取checkbox控件的值</para>
        /// <para>   true:[非空]  false:[null | "" | "off" | "0"]</para>
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>参数值</returns>
        public static Boolean RequestBoolean(String sParam, Boolean defaultValue)
        {
            Boolean bValue;
            String sValue = RequestString(sParam);

            if (!Boolean.TryParse(sValue, out bValue))
            {
                Int32 i;
                if (Int32.TryParse(sValue, out i))
                    bValue = i.ToBoolean();
                else
                    return defaultValue;
            }

            return bValue;
        }
        /// <summary>
        /// 把请求参数转换为 [可空Boolean] 类型 
        /// <para>注释：</para>
        /// <para>  这个方法主要用于获取checkbox控件的值</para>
        /// <para>   true:[非空]  false:[null | "" | "off" | "0"]</para>
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>参数值</returns>
        public static Boolean? RequestBoolean(String sParam, Boolean? defaultValue)
        {
            Boolean iValue;
            String sValue = RequestString(sParam);

            if (!Boolean.TryParse(sValue, out iValue))
                return defaultValue;

            return iValue;
        }
        #endregion

        #region Enum 类型参数

        /// <summary>
        /// 获取 Enum 类型参数
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns></returns>
        public static TEnum RequestEnum<TEnum>(String sParam) where TEnum : struct
        {
            return sParam.ToEnum<TEnum>();
        }
        #endregion

        #region File 类型参数 [上传控件]


        /// <summary>
        /// 获取 File 类型参数 [上传控件]
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns></returns>
        public static HttpPostedFile RequestFile(String sParam)
        {
            return HttpContext.Current.Request.Files[sParam];
        }
        #endregion

        #region Decimal 类型参数


        /// <summary>
        /// 把请求参数转换为Decimal类型
        /// <para>注释：</para>
        /// <para>  这个方法主要用于获取 moneybox控件 的值</para>
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns>参数值</returns>
        public static Decimal RequestMoney(String sParam)
        {
            Decimal dValue = 0M;
            String sValue = RequestString(sParam);

            if (sValue.HasValue())
            {
                sValue = sValue.Replace(",", "");
                Decimal.TryParse(sValue, out dValue);
            }

            return dValue;
        }

        /// <summary>
        /// 把请求参数转换为Decimal类型
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns>参数值</returns>
        public static Decimal RequestDecimal(String sParam)
        {
            return RequestDecimal(sParam, 0M);
        }
        /// <summary>
        /// 把请求参数转换为Decimal类型
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <param name="defaultValue"></param>
        /// <returns>参数值</returns>
        public static Decimal RequestDecimal(String sParam, Decimal defaultValue)
        {
            Decimal dValue;
            String sValue = RequestString(sParam);

            if (!Decimal.TryParse(sValue, out dValue))
                return defaultValue;

            return dValue;
        }
        #endregion

        #region Double 类型参数

        /// <summary>
        /// 把请求参数转换为 Double 类型
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns>参数值</returns>
        public static Double RequestDouble(String sParam)
        {
            Double dValue = 0D;
            String sValue = RequestString(sParam);

            if (sValue.HasValue())
            {
                Double.TryParse(sValue, out dValue);
            }

            return dValue;
        }
        /// <summary>
        /// 把请求参数转换为 Double 类型
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns>参数值</returns>
        public static Double RequestPerCent(String sParam)
        {
            return RequestDouble(sParam) / 100;
        }
        #endregion

        #endregion

        #region 获取请求方法名称
        /// <summary>
        /// 检测是否AjaxFun变量
        /// </summary>
        /// <returns></returns>
        public static Boolean ExistAjaxFun(out String fun)
        {
            fun = RequestString(AjaxFun);
            return !String.IsNullOrWhiteSpace(fun) ? true : false;
        }

        /// <summary>
        /// 过滤掉ajax提交的参数中某些值
        /// </summary>
        /// <param name="arr">context.Request.Form.AllKeys的值</param>
        /// <returns></returns>
        public static object[] GetParams(String[] arr)
        {
            List<String> list = arr.ToList();
            if (list.Contains(AjaxFun))
            {
                list.Remove(AjaxFun);
            }
            else if (list.Contains("__VIEWSTATE"))//检测是否有服务器控件
            {
                return null;
            }

            if (list.Count > 0)
            {
                List<String> listValue = new List<String>();
                foreach (String str in list)
                {
                    listValue.Add(RequestString(str));
                }
                return listValue.ToArray<object>();
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}